package zhihuilan.controlle.wechatMini;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zhihuilan.common.WxMiniResponse;
import zhihuilan.dto.MyFactoryProductDto;
import zhihuilan.entity.MyFactoryProduct;
import zhihuilan.entity.MyShop;
import zhihuilan.params.query.ProductQueryParam;
import zhihuilan.service.MyCategoryProductService;
import zhihuilan.service.MyFactoryProductService;
import zhihuilan.service.MyShopService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/app/Index")
public class SearchProductController {

   @Autowired
   MyFactoryProductService myFactoryProductService;

   @Autowired
    MyShopService myShopService;

    /**
     * @api {GET} /Index/searchproduct 找货
     * @apiParam {string}  guid 登录标识
     * @apiParam {string}  pid1name 名称
     * @apiParam {string}  pid2name 电压
     * @apiParam {string}  pid3name 型号
     * @apiParam {string}  pid5name 规格
     * @apiParam {string}  factoryname 品牌（现货使用）
     * @apiParam {string}  districtid 城市id（现货使用）
     * @apiParam {string}  pagesize 页面大小
     * @apiParam {string}  page 页码
     */
   @PostMapping("/searchproduct")
   public WxMiniResponse searchproduct(@RequestBody ProductQueryParam queryParam) {
       Map map = myFactoryProductService.selectSearchProductPage(queryParam);
       List<MyFactoryProduct> list= (List<MyFactoryProduct>) map.get("data");
       List<MyFactoryProductDto> dtoList = list.stream().map(po->{
           MyFactoryProductDto myFactoryProductDto = new MyFactoryProductDto();
           BeanUtils.copyProperties(po,myFactoryProductDto);
           myFactoryProductDto.setFactory(myShopService.selectShopByById(po.getShopid()));
           return myFactoryProductDto;
       }).collect(Collectors.toList());
        map.put("data",dtoList);
       System.out.println(map);
        return WxMiniResponse.ok(map);
   }
}
